﻿using System.Collections.Generic;

namespace Ex03.GarageLogic
{
    public class Garage
    {
        public const string k_WrongEnergyType = "Can't fuel an electric vehicle or recharge a fuel-based vehicle";
        private readonly List<Vehicle> r_Vehicles;

        public Garage()
        {
            r_Vehicles = new List<Vehicle>();
        }

        public void InsertVehicleIntoGarage(string i_PlateNumber, out bool o_AlreadyInside)
        {
            Vehicle vehicle;
            GetVehicle(i_PlateNumber, out vehicle);

            if (vehicle == null)
            {
                o_AlreadyInside = false;
            }
            else
            {
                o_AlreadyInside = true;
                vehicle.CurrentStatusInGarage = Vehicle.eVehicleStatusInGarage.Fixing;
            }
        }

        public List<Vehicle> FilterListOfVehicles(Vehicle.eVehicleStatusInGarage i_Status)
        {
            List<Vehicle> filteredList = new List<Vehicle>();

            foreach (Vehicle vehicle in r_Vehicles)
            {
                if (vehicle.CurrentStatusInGarage == i_Status)
                {
                    filteredList.Add(vehicle);
                }
            }

            return filteredList;
        }

        public void ChangeVehicleStatus(string i_PlateNumber, Vehicle.eVehicleStatusInGarage i_Status)
        {
            Vehicle vehicle;
            GetVehicle(i_PlateNumber, out vehicle);

            vehicle.CurrentStatusInGarage = i_Status;    
        }

        public void InflateWheels(string i_PlateNumber)
        {
            Vehicle vehicle;
            GetVehicle(i_PlateNumber, out vehicle);

            vehicle.InflateAllWheels();
        }

        public void RefuelVehicle(string i_PlateNumber, FuelEngine.eFuelType i_FuelType, float i_Amount) 
        {
            Vehicle vehicle;
            GetVehicle(i_PlateNumber, out vehicle);
            
            IFuelBased fuelVehicle = vehicle as IFuelBased;
            if (fuelVehicle == null)
            {
                throw new System.ArgumentException(k_WrongEnergyType);
            }

            fuelVehicle.Refuel(i_Amount, i_FuelType, ref vehicle);
        }

        public void RechargeBatteryInVehicle(string i_PlateNumber, float i_NumOfHours)
        {
            Vehicle vehicle;
            GetVehicle(i_PlateNumber, out vehicle);

            IElectricBased electricVehicle = vehicle as IElectricBased;
            if (electricVehicle == null)
            {
                throw new System.ArgumentException(k_WrongEnergyType);
            }

            electricVehicle.Recharge(i_NumOfHours, ref vehicle);
        }
    
        public bool GetVehicle(string i_PlateNumber, out Vehicle o_Vehicle)
        {
            bool found = false;
            o_Vehicle = null;

            foreach (Vehicle vehicle in r_Vehicles)
            {
                if (vehicle.PlateNumber == i_PlateNumber)
                {
                    o_Vehicle = vehicle;
                    found = true;
                }
            }

            return found;
        }

        public List<Vehicle> Vehicles
        {
            get { return r_Vehicles; }
        }
    }
}
